<?php
namespace services;
use addons\epay\library\Service;
require_once "../vendor/alipaysdk/aop/AopClient.php";
require_once "../vendor/alipaysdk/aop/AopCertClient.php";
require_once "../vendor/alipaysdk/aop/AopCertification.php";
require_once "../vendor/alipaysdk/aop/AlipayConfig.php";
require_once "../vendor/alipaysdk/aop/request/AlipayFundTransUniTransferRequest.php";
class AlipayService{
    // appid
    private $appId = "2021002152652439";
    //
    private $seller_id = "2088141761276713";
    // 应用私钥
    private $rsaPrivateKey = "";
    // 应用公钥证书文件路径
    private $appCertPath = "";
    // 支付宝公钥证书文件路径
    private $alipayPublicCertPath = "";
    // 支付宝根证书文件路径
    private $rootCertPath = "";
    public function __construct() {
        $this->options = array();
        $config = Service::getConfig('alipay');
        $this->appId = $config['app_id'];
        $this->rsaPrivateKey = $config['private_key'];
        $this->appCertPath = $config['app_cert_public_key'];
        $this->alipayPublicCertPath = $config['ali_public_key'];
        $this->rootCertPath = $config['alipay_root_cert'];
    }



    public function transfer($param)
    {       
        $alipayConfig = new \AlipayConfig();
        $alipayConfig->setPrivateKey($this->rsaPrivateKey);
        $alipayConfig->setServerUrl("https://openapi.alipay.com/gateway.do");
        $alipayConfig->setAppId($this->appId);
        $alipayConfig->setCharset("UTF-8");
        $alipayConfig->setSignType("RSA2");
        $alipayConfig->setEncryptKey("");
        $alipayConfig->setFormat("json");
        $alipayConfig->setAppCertPath($this->appCertPath);
        $alipayConfig->setAlipayPublicCertPath($this->alipayPublicCertPath);
        $alipayConfig->setRootCertPath($this->rootCertPath);
        $alipayClient = new \AopCertClient($alipayConfig);
        $alipayClient->isCheckAlipayPublicCert = true;
        $request = new \AlipayFundTransUniTransferRequest();
        $param['biz_scene'] = 'DIRECT_TRANSFER';
        $param['product_code'] = 'TRANS_ACCOUNT_NO_PWD';
        $request->setBizContent(json_encode($param,JSON_UNESCAPED_UNICODE));
        // $request->setBizContent("{".
        //     "\"out_biz_no\":\"43545676\",".
        //     "\"remark\":\"201905代发\",".
        //     "\"business_params\":\"{\\\"payer_show_name_use_alias\\\":\\\"true\\\"}\",".
        //     "\"biz_scene\":\"DIRECT_TRANSFER\",".
        //     "\"payee_info\":{".
        //         "\"identity\":\"2088123412341234\",".
        //         "\"identity_type\":\"ALIPAY_USER_ID\",".
        //         "\"name\":\"黄龙国际有限公司\"".
        //     "},".
        //     "\"trans_amount\":\"23.00\",".
        //     "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
        //     "\"order_title\":\"201905代发\"".
        // "}");
        $responseResult = $alipayClient->execute($request);
        $responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
        $response = $responseResult->$responseApiName;
        return $response;
    }

}
